<?php
namespace app\admin\controller;
use think\Controller;
use think\Db;
use think\Session;
use think\Cookie;

class Cloudaccountwithdraw
{

    private $app_dealer_id = '';
    private $app_broker_id = '';
    private $app_key = '';
    private $app_des3_key = '';
    private $app_private_key = '';
    private $yzh_public_key = '';

    public function __construct(){
        $config_arr = require __DIR__."/../../../adminconfig/yzhconfig.php";
        $this->app_dealer_id = $config_arr['app_dealer_id'];
        $this->app_broker_id = $config_arr['app_broker_id'];
        $this->app_key = $config_arr['app_key'];
        $this->app_des3_key = $config_arr['app_des3_key'];
        $this->app_private_key = $config_arr['app_private_key'];
        $this->yzh_public_key = $config_arr['yzh_public_key'];
    }

    /**
     * 执行微信提现
     * User:@hang
     * Date:2023-09-18 20:35
     */
    public function wxyunzhanghu($order_num,$name,$id_card,$openid,$phone,$pay_money,$notify_url){
//        return ['code'=>'200','msg'=>'微信确认提现操作成功','data'=>array()];
//        var_dump($order_num);var_dump($name);var_dump($id_card);var_dump($openid);var_dump($phone);var_dump($pay_money);var_dump($notify_url);die;
        $config = \Yzh\Config::newFromArray(array(
            "app_dealer_id" => $this->app_dealer_id,
            "app_broker_id" => $this->app_broker_id,
            "app_key" => $this->app_key,
            "app_des3_key" => $this->app_des3_key,
            "app_private_key" => $this->app_private_key,
            "yzh_public_key" => $this->yzh_public_key,
        ));
        $client = new \Yzh\PaymentClient($config);
        $client->setEnv(\Yzh\PaymentClient::ENV_PROD);// 沙箱环境使用 ENV_SANDBOX
        // 配置请求参数
        $request = new \Yzh\Model\Payment\CreateWxpayOrderRequest(array(
            "order_id" => $order_num,
            "dealer_id" => $this->app_dealer_id,
            "broker_id" => $this->app_broker_id,
            "real_name" => $name,
            "id_card" => $id_card,
            "openid" => $openid,
            "phone_no" => $phone,
            "pay" => $pay_money,
            "pay_remark" => "一堆小车微信提现",
            "notify_url" => $notify_url,
            "wx_app_id" => "",
            "wxpay_mode" => "transfer",
            "project_id" => ""
        ));
        $request->setRequestID('WXTX'.time().rand(1111,9999));
        try {
            $response = $client->createWxpayOrder($request);
            // var_dump($response);die;
            if ($response->isSuccess()) {
                if($response->getCode() == '0000'){
                    return ['code'=>'200','msg'=>'微信确认提现操作成功','data'=>array()];
                }
                return ['code'=>'500','msg'=>'微信确认提现操作错误','data'=>array()];
            } else {
                return ['code'=>'500','msg'=>'微信确认提现操作失败：'.$response->getCode().$response->getMessage(),'data'=>array()];
            }
        } catch(Exception $e) {
            return ['code'=>'500','msg'=>'微信提现异常：'.$e->getMessage(),'data'=>array()];
        }
    }


    /**
     * 执行支付宝提现
     * User:@hang
     * Date:2023-09-18 20:35
     */
    public function zfbyunzhanghu($order_num,$name,$id_card,$zfbzh,$phone,$pay_money,$notify_url){
//        return ['code'=>'200','msg'=>'支付宝确认提现操作成功','data'=>array()];
//        var_dump($order_num);var_dump($name);var_dump($id_card);var_dump($zfbzh);var_dump($phone);var_dump($pay_money);var_dump($notify_url);die;
        // 配置基础信息
        $config = \Yzh\Config::newFromArray(array(
            "app_dealer_id" => $this->app_dealer_id,
            "app_broker_id" => $this->app_broker_id,
            "app_key" => $this->app_key,
            "app_des3_key" => $this->app_des3_key,
            "app_private_key" => $this->app_private_key,
            "yzh_public_key" => $this->yzh_public_key,
            "sign_type" => "rsa"
        ));
        $client = new \Yzh\PaymentClient($config);
        $client->setEnv(\Yzh\PaymentClient::ENV_PROD);// 沙箱环境使用 ENV_SANDBOX
// 配置请求参数
        $request = new \Yzh\Model\Payment\CreateAlipayOrderRequest(array(
            "order_id" => $order_num,
            "dealer_id" => $this->app_dealer_id,
            "broker_id" => $this->app_broker_id,
            "real_name" => $name,
            "card_no" => $zfbzh,
            "id_card" => $id_card,
            "phone_no" => $phone,
            "pay" => $pay_money,
            "pay_remark" => "一堆小车支付宝提现",
            "notify_url" => $notify_url,
            "project_id" => ""
        ));
        $request->setRequestID('ZFBTX'.time().rand(1000,9999));
        try {
            $response = $client->createAlipayOrder($request);
            if ($response->isSuccess()) {
                if($response->getCode() == '0000'){
                    return ['code'=>'200','msg'=>'支付宝确认提现操作成功','data'=>array()];
                }
                return ['code'=>'500','msg'=>'支付宝确认提现操作错误','data'=>array()];
            } else {
                return ['code'=>'500','msg'=>'支付宝确认提现操作失败：'.$response->getCode().$response->getMessage(),'data'=>array()];
            }
        } catch(Exception $e) {
            return ['code'=>'500','msg'=>'支付宝提现异常：'.$e->getMessage(),'data'=>array()];
        }
    }





}
